package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 * Servlet implementation class PrimerServlet
 */
public class RequestInfo extends HttpServlet {



	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public RequestInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	 public void doGet(HttpServletRequest request, HttpServletResponse response)
			    throws IOException, ServletException
			    {
			        response.setContentType("text/html");
			        PrintWriter out = response.getWriter();
			        out.println("<html>");
			        out.println("<body>");
			        out.println("<head>");			        
				    String param1 = request.getParameter("param1");
				    String param2 = request.getParameter("param2");
			        if("enviar".equals(request.getParameter("boton"))){
			        	 out.println("<h3>Request Information Example por Get por formulario</h3></br>");
					     String nombre = request.getParameter("user");
					     String password = request.getParameter("password");
					     out.println("user: " + nombre + "</br>");
					     out.println("Password: " + password + "</br>");
					     
					     out.println("<br/><h3>Variables de Session</h3></br>");
					     
					     HttpSession session = request.getSession(true);
					     session.setAttribute("user",nombre);
					     out.println(session.getAttribute("user"));

					        // print session info

					        Date created = new Date(session.getCreationTime());
					        Date accessed = new Date(session.getLastAccessedTime());
					        out.println("ID " + session.getId());
					        out.println("Created: " + created);
					        out.println("Last Accessed: " + accessed);

					       
			        }else if(param1!=null || param2!=null){
			        	 out.println("<h3>Request Information Example por Get con parametros</h3></br>");
					     out.println("Nombre: " + param1 + "</br>");
					     out.println("Password: " + param2 + "</br>");
					     
					     out.println("<br/><h3>Parametros dinamicamente</h3></br>");
					     Map param = request.getParameterMap();
					     Iterator it = param.entrySet().iterator();
					     while (it.hasNext()) {
					         Map.Entry e = (Map.Entry) it.next();
					         out.println("[" + e.getKey() + "] :" + e.getValue() + "</br>");
					     }
	
			        }else{
			        	 out.println("<title>Request Information Example por Get</title>");
					        out.println("</head>");
					        out.println("<body>");
					        out.println("<h3>Request Information Example por Get</h3></br>");
					        out.println("Method: " + request.getMethod() + "</br>");
					        out.println("Request URI: " + request.getRequestURI() + "</br>");
					        out.println("Protocol: " + request.getProtocol() + "</br>");
					        out.println("PathInfo: " + request.getPathInfo() + "</br>");
					        out.println("Remote Address: " + request.getRemoteAddr() + "</br>");
					        out.println("Attribute Name: " + request.getAttributeNames() + "</br>");
					        out.println("Author Type: " + request.getAuthType() + "</br>");
					        out.println("Character Encoding: " + request.getCharacterEncoding() + "</br>");
					        out.println("Local Name: " + request.getLocalName());
					        
					        out.println("<br/><h3>Request Information General</h3></br>");
					        Enumeration e = request.getHeaderNames();
					        while (e.hasMoreElements()) {
					            String name = (String)e.nextElement();
					            String value = request.getHeader(name);
					            out.println(name + " = " + value + "</br>");
					        }
					        
			        }
			        

			        out.println("</body>");
			        out.println("</html>");
			    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//doGet(request, response);
		String nombre = request.getParameter("nombre");
		String password = request.getParameter("password");
		 response.setContentType("text/html");
	        PrintWriter out = response.getWriter();
	        out.println("<html>");
	        out.println("<body>");
	        out.println("<head>");
	        out.println("<title>Request Information Example por Post</title>");
	        out.println("</head>");
	        out.println("<body>");
	        out.println("<h3>Request Information Example por Post</h3></br>");
	        out.println("Nombre: " + nombre + "</br>");
	        out.println("Password: " + password + "</br>");
	        
	        out.println("</body>");
	        out.println("</html>");
		
	}

}
